package com.xingfly;

import com.xingfly.model.*;
import com.xingfly.repository.PermissionRepository;
import com.xingfly.repository.RelationRepository;
import com.xingfly.repository.RoleRepository;
import com.xingfly.service.*;
import com.xingfly.util.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * Created by SuperS on 2016/11/29.
 */
@Component
public class InitData implements CommandLineRunner {
    @Autowired
    AccountService accountService;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    PermissionRepository permissionRepository;
    @Autowired
    CommentService commentService;
    @Autowired
    LikeService likeService;
    @Autowired
    RelationRepository relationRepository;
    @Autowired
    EventService eventService;
    @Autowired
    PostService postService;
    @Autowired
    TagService tagService;
    @Autowired
    InterestService interestService;
    @Autowired
    MailService mailService;
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private FeedService feedService;


    @Override
    public void run(String... strings) throws Exception {


        Role admin = new Role();
        admin.setRoleName("ROLE_ADMIN");
        roleRepository.save(admin);

        Role user = new Role();
        user.setRoleName("ROLE_USER");
        roleRepository.save(user);

        Permission permission = new Permission();
        permission.setPermissionName("READ");
        permission.setRole(roleRepository.findOne(1L));
        permissionRepository.save(permission);

        Permission permission2 = new Permission();
        permission2.setPermissionName("WRITE");
        permission2.setRole(roleRepository.findOne(1L));
        permissionRepository.save(permission2);

        User fpf = new User();
        fpf.setMail("fpf@qq.com");
        fpf.setPassword("fpf");
        fpf.setIntro("fpf");
        fpf.setAvatar("http://pic.xingfly.com/tx2.jpg");
        fpf.setNickname("fpf");
        fpf.setUsername("fpf");
        fpf.setPhone("186");
        fpf.setTokenExpired(true);
        fpf.setEnable(true);
        fpf.setRoles(Collections.singletonList(roleRepository.findOne(1L)));
        accountService.save(fpf);

        User updateFPF = accountService.find("fpf");
        updateFPF.setNickname("SuperS");
        accountService.update(updateFPF);

        User wl = new User();
        wl.setMail("wl@qq.com");
        wl.setPassword("wl");
        wl.setAvatar("http://pic.xingfly.com/tx1.jpg");
        wl.setIntro("wl");
        wl.setNickname("wl");
        wl.setUsername("wl");
        wl.setPhone("137");
        wl.setTokenExpired(true);
        wl.setEnable(true);
        wl.setRoles(Collections.singletonList(roleRepository.findOne(2L)));
        accountService.save(wl);


        Comment comment = new Comment();
        comment.setContent("第一条评论");
        comment.setAuthor(accountService.find(1L));
        comment.setCommentObjectId(1L);
        comment.setCommentObjectType(Type.OBJECT_TYPE_POST);
        commentService.create(comment);
        Comment comment2 = new Comment();
        comment2.setContent("第二条评论");
        comment2.setAuthor(accountService.find(1L));
        comment2.setCommentObjectId(1L);
        comment2.setCommentObjectType(Type.OBJECT_TYPE_POST);
        commentService.create(comment2);

        likeService.like(1L, Type.OBJECT_TYPE_POST, 1L);
        likeService.like(2L, Type.OBJECT_TYPE_POST, 1L);
        likeService.undoLike(1L, Type.OBJECT_TYPE_POST, 1L);

        Tag tag = new Tag();
        tag.setTitle("XingFly");
        tag.setCover("XingFly");
        tagService.create(tag);

        Relation relation = new Relation();
        relation.setObjectId(1L);
        relation.setObjectType(Type.OBJECT_TYPE_POST);
        relation.setTag(tagService.findTagById(1L));
        relationRepository.save(relation);
        Relation relation1 = new Relation();
        relation1.setObjectId(1L);
        relation1.setObjectType(Type.OBJECT_TYPE_POST);
        relation1.setTag(tagService.findTagById(2L));
        relationRepository.save(relation1);


        Post post = new Post();
        post.setTitle("第一个Post");
        post.setContent("Hello,XingFly!");
        post.setAuthor(accountService.find(1L));
        postService.create(post, "fpf xingfly first");

//        mailService.sendAccountActivationEmail("549052145@qq.com", "123456");


        Post post1 = new Post();
        post1.setTitle("第二个Post");
        post1.setContent("Hello,FPF!");
        post1.setAuthor(accountService.find(1L));
        postService.create(post1, "你好 樊鹏飞");

        Post post2 = new Post();
        //再创建几个post
        post2.setTitle("第三个Post");
        post2.setContent("Hello,HXX");
        post2.setAuthor(accountService.find(1L));

        postService.create(post2, "胡鑫鑫");

        Post post3 = new Post();
        post3.setTitle("第四个Post");
        post3.setContent("Hello,WL");
        post3.setAuthor(accountService.find(1L));

        postService.create(post3, "王莉");

        Post post4 = new Post();
        post4.setTitle("第五个Post");
        post4.setContent("Hello,WD");
        post4.setAuthor(accountService.find(1L));

        postService.create(post4, "王丁");

        Post post5 = new Post();
        post5.setTitle("第六个Post");
        post5.setContent("Hello,New World!");
        post5.setAuthor(accountService.find(1L));

        postService.create(post5, "新世界");


        eventService.create(Type.OBJECT_TYPE_POST, postService.findById(1L));
        eventService.delete(1L);
        eventService.delete(Type.OBJECT_TYPE_POST, 1L);


        tagService.create("标签二");

        System.out.println(tagService.getHasCoverRecommendTags().toString());
        System.out.println(tagService.getNotCoverRecommendTags());
        System.out.println(tagService.getTagId("标签二"));
        System.out.println(tagService.findTagById(1L));


        interestService.create(1L, 1L);
        interestService.create(1L, 2L);
        interestService.create(2L, 1L);
        interestService.delete(1L, 1L);

//        Notification n1 = new Notification();
//        n1.setEvent(eventService.getEvent(Type.OBJECT_TYPE_POST, 1L));
//        n1.setNotificationType(Type.NOTIFY_TYPE_SYSTEM);
//        n1.setNotifiedUser(accountService.find(1L));
//        n1.setNotifier(accountService.find(2L));
//        n1.setStatus(1);
//        n1.setNotificationId(1L);
//        notificationService.doNotify(n1);
//        Notification n2 = new Notification();
//        n2.setEvent(eventService.getEvent(Type.OBJECT_TYPE_POST, 2L));
//        n2.setNotificationType(Type.NOTIFY_TYPE_SYSTEM);
//        n2.setNotifiedUser(accountService.find(1L));
//        n2.setNotifier(accountService.find(2L));
//        n2.setStatus(1);
//        n2.setNotificationId(1L);
//        notificationService.doNotify(n2);
//        Notification n3 = new Notification();
//        n3.setEvent(null);
//        n3.setNotificationType(Type.NOTIFY_TYPE_COMMENT);
//        n3.setNotifiedUser(accountService.find(1L));
//        n3.setNotifier(accountService.find(2L));
//        n3.setStatus(1);
//        notificationService.doNotify(n3);
//        System.out.println(notificationService.getNotificationsCount(1L));
//        System.out.println(notificationService.getNotificationsCount(1L));
//
//        System.out.println(feedService.getFeedsOfOfPage(1L, 10));

//        System.out.println(postService.findPostsByUserIdOrderByDesc(1L, 0, 5).toString());

//        邮件发送服务
//        mailService.sendResetPwdEmail("857299269@qq.com","234567");


    }
}
